package com.example.androiddemo_bsl;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import cn.bvin.lib.app.RequestActivity;
import cn.bvin.lib.app.RequestHolderFragment;
import cn.bvin.lib.debug.SimpleLogger;
import cn.bvin.lib.module.net.MapParam;
import cn.bvin.lib.module.net.volley.BaseRequest;
import cn.bvin.lib.module.net.volley.GsonRequest;

import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;

public class MainActivity extends RequestActivity<UpdateModel> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parserIntent(getIntent());
        initData();
        initViews(R.layout.activity_main);
        startPullData();
    }
    
    
    
    @Override
    public void parserIntent(Intent intent) {
        super.parserIntent(intent);
    }



    @Override
    public void initData() {
        super.initData();
    }



    @Override
    public void initViews(View rootView) {
        super.initViews(rootView);
        mRequestHolderFragment = new RequestHolderFragment();
        getSupportFragmentManager().beginTransaction()
        .replace(R.id.container, mRequestHolderFragment).commit();
    }



    private void startPullData() {
        String url = "http://lite.phone.bacic5i5j.com/PhoneOA/ReqWeb/System/System.ashx";
        MapParam mp = new MapParam();
        mp.put("Method", "UpdateApk");
        GsonRequest<UpdateModel> request = new GsonRequest<UpdateModel>(Method.POST, url, mp.get(),
                UpdateModel.class, this, this);
        addRequest(request);
    }
    
    @Override
    public void onRequestStart(Request<UpdateModel> resquest) {
        super.onRequestStart(resquest);
        SimpleLogger.log_e("onRequestStart", ((BaseRequest<UpdateModel>)resquest).getUrlWithParams());
    }

    @Override
    public void onRequestSuccess(UpdateModel result) {
        super.onRequestSuccess(result);
        SimpleLogger.log_e("onRequestSuccess", result.note);
    }

    @Override
    public void onRequestFailure(VolleyError error) {
        super.onRequestFailure(error);
        if (error instanceof NoConnectionError) {// 错误界面
            SimpleLogger.log_e("onRequestFailure", "服务器连接失败");
        } else if (error instanceof TimeoutError) {
            SimpleLogger.log_e("onRequestFailure", "网络超时");
        } else if (error instanceof ParseError) {
            SimpleLogger.log_e("onRequestFailure", "数据解析异常");
        } else if (error instanceof ServerError) {
            SimpleLogger.log_e("onRequestFailure", "服务器异常"+error.toString());
        } else {
            SimpleLogger.log_e("onRequestFailure", "其他错误");
        }
        
    }

}
